package com.etheart.lyric.htmlparser.action.parse;

import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.htmlparser.Parser;
import org.htmlparser.util.ParserException;

import com.etheart.lyric.htmlparser.service.baidu.BaiduAlbumListParseService;
import com.etheart.lyric.htmlparser.service.baidu.BaiduAlbumParseService;
import com.etheart.lyric.htmlparser.service.baidu.BaiduSingerParseService;
import com.etheart.lyric.htmlparser.service.baidu.BaiduSongParseService;
import com.etheart.lyric.htmlparser.util.HtmlparserUtils;
import com.etheart.model.entity.Album;
import com.etheart.model.entity.Resource;
import com.etheart.model.entity.Singer;
import com.etheart.model.entity.Song;
import com.etheart.music.service.AlbumService;
import com.etheart.music.service.ResourceService;
import com.etheart.music.service.SingerService;
import com.etheart.music.service.SongService;

public class BaiduParseThread extends Thread {
	private static final Log logger = LogFactory.getLog(BaiduParseThread.class);
	protected BaiduSingerParseService singerParseService;
	protected BaiduAlbumParseService albumParseService;
	protected BaiduAlbumListParseService albumListParseService;
	protected BaiduSongParseService songParseService;
	protected SingerService singerService;
	protected AlbumService albumService;
	protected SongService songService;
	protected ResourceService resourceService;
	private Session session;
	
	public String singerNetId;
	public boolean isProcess=false;
	public String areaAndSex;
	
	protected String rootDir;
	protected String rootUrl="http://ting.baidu.com/";
	
	@Override
	public synchronized void start() {
		isProcess=true;
	}
	@Override
	public void run() {
		session=singerService.openSession();
		System.out.println("HibernateSessionFilter begin transaction");
		session.beginTransaction(); //开始事务
		
		//解析歌手
		Parser singerParser = HtmlparserUtils.getParser(rootDir+"/artist/"+singerNetId+"/introduction.html", rootUrl+"artist/"+singerNetId+"/introduction");
		Singer singer = singerParseService.getSingerFromParser(singerParser, singerNetId);
		if (singerService.findByNetId(singer.getNetId())==null) {
			String[] str = areaAndSex.split("-");
			singer.setArea(str[0]);
			singer.setSex(str[1]);
			singerService.addSinger(singer);
		}else{
			singer = singerService.findByNetId(singer.getNetId());
		}
		//解析专辑列表
		Parser albumListparser = HtmlparserUtils.getParser(rootDir+"/artist/"+singerNetId+"/album.html", rootUrl+"artist/"+singerNetId+"/album");
		try {
			List<String> albumUrlList = albumListParseService.parseAlbumListPage(albumListparser, rootDir+"/artist/"+singerNetId+"/album.html", rootUrl+"artist/"+singerNetId+"/album");
			for (String url : albumUrlList) {
				String albumid = url.replace("/album/", "");
				Parser albumParser = saveAlbum(singer, albumid);
				saveSong(albumParser);
				
			}
		} catch (ParserException e) {
			logger.error("eeeeeeeeeeeeeeeeeeee:专辑列表解析失败,singerNetId:"+singerNetId);
			error();
		}
		
		
		
		
		System.out.println("HibernateSessionFilter commit transaction");
		session.getTransaction().commit();
		session.close();
		isProcess=false;
	}
	private void saveSong(Parser albumParser)  {
		//解析歌曲
		try {
			List<Song> songs = albumParseService.getSongs(albumParser);
			for (Song song : songs) {
				//http://ting.baidu.com/song/13736238
				Parser songParser = HtmlparserUtils.getParserFromNetWork( rootUrl+"song/"+song.getNetId());
				songParseService.fillSong(songParser, song);
			}
			
			for (Song song : songs) {
				if (songService.findByNetId(song.getNetId())==null) {
					songService.addSong(song);
					for (Resource resource : song.getResources()) {
						resourceService.addResource(resource);
					}
				}
			}
			
		} catch (Exception e) {
			logger.error("eeeeeeeeeeeeeeee:歌曲解析异常");
		}
		
	}
	private Parser saveAlbum(Singer singer, String albumid) {
		//解析专辑
		Parser albumParser = HtmlparserUtils.getParser(rootDir+"/album/"+albumid+".html", rootUrl+"album/"+albumid);
		Album album = albumParseService.getAlbum(albumParser, albumid);
		album.setSinger(singer);
		if (albumService.findByNetId(album.getNetId())==null) {
			albumService.addAlbum(album);
		}else{
			album = albumService.findByNetId(album.getNetId());
		}
		return albumParser;
	}
	private void error() {
		session.getTransaction().rollback();
		isProcess=false;
		session.close();
	}


}
